package org.kotboot.module;/**
 * Created by jisong.sun on 2017/9/14.
 */

import com.alibaba.fastjson.annotation.JSONField;
import org.kotboot.enums.ResCode;
import org.kotboot.kit.K;

/**
 * jisong.sun 2017/9/14 10:26
 */
public class R<T> {
    private T obj;
    private ResCode code;
    private String msg;
    private String status;


    @JSONField(serialize = false)
    public boolean isOK(){
        return "suc".equals(status);
    }

    @JSONField(serialize = false)
    public boolean isErr(){
        return "err".equals(status);
    }

    public static R OK() {
        return R.OK(null);
    }

    public static R OK(Object obj){
        return R.OK("操作成功",obj);
    }

    public static R OK(String msg,Object obj){
        R r = new R();
        r.setCode(ResCode.SUC);
        r.setMsg(msg);
        r.setObj(obj);
        r.setStatus("suc");
        return r;
    }

    public static R ER(ResCode code, String msg, Object obj){
        R r = new R();
        r.setCode(code);
        r.setMsg(msg);
        r.setObj(obj);
        r.setStatus("err");
        return r;
    }

    public static R ER(String msg){
        return ER(ResCode.ERR,msg,null);
    }

    public static R ER(String msg ,Object obj){
        return ER(ResCode.ERR,msg,obj);
    }


    public T getObj() {
        return obj;
    }

    public void setObj(T obj) {
        this.obj = obj;
    }

    public ResCode getCode() {
        return code;
    }

    public void setCode(ResCode code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return K.toJson(this);
    }

}
